package com.ruoyi.modules.appController;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.modules.admin.service.ISysConfigService;
import com.ruoyi.modules.domain.DyAgentUser;
import com.ruoyi.modules.domain.vo.AppDyAgentUserVo;
import com.ruoyi.modules.domain.vo.SetPasswordDto;
import com.ruoyi.modules.service.IDyAgentUserService;
import com.ruoyi.modules.service.INumRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 代理Controller
 *
 * @author ruoyi
 * @date 2022-01-22
 */
@Api(tags = "app - 代理")
@RestController
@RequestMapping("/app/dyAgentUser")
@Slf4j
public class AppDyAgentUserController extends BaseController {
    @Autowired
    private INumRecordService numRecordService;
    @Autowired
    private IDyAgentUserService dyAgentUserService;
    @Autowired
    ISysConfigService iSysConfigService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value="代理用户信息", notes="代理用户信息")
    @GetMapping(value = "/getAgentUserInfo")
    @ApiImplicitParams({
    })
    public Result<AppDyAgentUserVo> getAgentUserInfo(HttpServletRequest req) {


        Long agentUserId = tokenService.getUserId(req);
        log.info("登录 userId: "+ agentUserId);


        AppDyAgentUserVo appDyAgentUserVo = dyAgentUserService.getAppDyAgentUserVo(agentUserId);


        appDyAgentUserVo.setHomeImg(iSysConfigService.selectConfigByKey("homeImg"));
        appDyAgentUserVo.setAuthorizationUrl(getAuthorizationUrl(agentUserId));


        appDyAgentUserVo.setPassword(null);
        return Result.ok(appDyAgentUserVo);
    }


    private String getAuthorizationUrl(Long agentUserId){
        return "https://duoduodan1688.cn/dyUserLogin.html?id=" + agentUserId;
    }




    @ApiOperation(value="根据账号验证码登录", notes="盲盒-根据账号验证码登录")
    @PostMapping(value = "/setPassword")

    public AjaxResult loginByPhone(HttpServletRequest req, @RequestBody SetPasswordDto setPasswordDto) {

        Long agentUserId = tokenService.getUserId(req);
        DyAgentUser dyAgentUser = dyAgentUserService.getById(agentUserId);
        if (dyAgentUser == null){
            return error("账号不存在！");
        }
        if (!dyAgentUser.getPassword().equals(setPasswordDto.getOldPassword())){
            return error("原密码错误！");
        }

        dyAgentUserService.update(new LambdaUpdateWrapper<DyAgentUser>()
            .set(DyAgentUser::getPassword, setPasswordDto.getNewPassword())
            .eq(DyAgentUser::getId, agentUserId));

        return success("修改成功");
    }


}
